<?php

namespace Kunfeng\Dify\Model\Datasets;

/**
 * 处理规则模型
 */
class ProcessRule
{
    private $mode;
    private $rules;

    public function __construct(array $data = [])
    {
        $this->mode = $data['mode'] ?? 'automatic';
        $this->rules = isset($data['rules']) ? ProcessRuleRules::fromArray($data['rules']) : null;
    }

    /**
     * 从数组创建
     */
    public static function fromArray(array $data): self
    {
        return new self($data);
    }

    /**
     * 创建自动处理规则
     */
    public static function createAutomatic(): self
    {
        return new self(['mode' => 'automatic']);
    }

    /**
     * 创建自定义处理规则
     */
    public static function createCustom(ProcessRuleRules $rules): self
    {
        return new self(['mode' => 'custom', 'rules' => $rules->toArray()]);
    }

    /**
     * 创建分层处理规则
     */
    public static function createHierarchical(ProcessRuleRules $rules): self
    {
        return new self(['mode' => 'hierarchical', 'rules' => $rules->toArray()]);
    }

    // Getters
    public function getMode(): string
    {
        return $this->mode;
    }

    public function getRules(): ?ProcessRuleRules
    {
        return $this->rules;
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        $data = ['mode' => $this->mode];
        if ($this->rules !== null) {
            $data['rules'] = $this->rules->toArray();
        }
        return $data;
    }
}
